//783. 二叉搜索树节点最小距离
//思路：深度优先遍历，中序遍历

#include <stdlib.h>
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};
void dfs(struct TreeNode* root, int* prev, int* min)
{
    if(NULL == root)
        return;
    
    dfs(root->left, prev, min);

    if(-1 == *prev)
    {
        *prev = root->val;
    }
    else
    {
        *min = fmin(*min-*prev, *min);
        *prev = root->val;
    }

    dfs(root->right, prev, min);
}
int minDiffInBST(struct TreeNode* root){
    int min = INT_MAX, * prev = -1;
    dfs(root, &prev, &min);
    
    return min;
}